/**
* Copyright (c) 2004-2013 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

package it.ciano.util;

import java.io.*;
import java.util.*;

/**
 * A static class to manage execution of external programs in java.
 * 
 * @author Luciano Xumerle
 * @version 0.0.1
 */
public class CmdExec
{

    /**
     * The method execute a command.
     * 
     * @param cmdline an array with command and parameters
     * @return an array with every line captured from the output
     */
    public static String[] commandExec( String[] cmdline )
    {
        ArrayList result = new ArrayList();
        String a[];
        try
        {
            String line;
            Process p = Runtime.getRuntime().exec( cmdline );
            BufferedReader input =
                new BufferedReader
                ( new InputStreamReader( p.getInputStream() ) );
            while ( ( line = input.readLine() ) != null )
                result.add( line );
            input.close();
            a = new String[ result.size() ];
            for ( int i = 0; i < result.size(); i++ )
                a[ i ] = ( String ) result.get( i );
            return a;
        }
        catch ( Exception err )
        {
            result.add( ( String ) err.getMessage() );
            a = new String[ result.size() ];
            for ( int i = 0; i < result.size(); i++ )
                a[ i ] = ( String ) result.get( i );
            return a;
        }
    }

}

